//
//  UIView+Frame.m
//  mapdemo
//
//  Created by qinghuan.lu on 16/11/10.
//  Copyright © 2016年 thomasho. All rights reserved.
//


#import "UIView+Frame.h"

static const CGFloat Epsilon = 0.000001;

@implementation UIView (Frame)

- (CGFloat)originX
{
	return self.frame.origin.x;
}

- (CGFloat)originY
{
	return self.frame.origin.y;
}

- (CGFloat)width
{
	return self.frame.size.width;
}

- (CGFloat)height
{
	return self.frame.size.height;
}

- (void)setOriginX:(CGFloat)originX
{
	CGRect frame = self.frame;
	
	if (fabs(frame.origin.x - originX) < Epsilon) {
		return;
	}
	else {
		frame.origin.x = originX;
		self.frame = frame;
	}
}

- (void)setOriginY:(CGFloat)originY
{
	CGRect frame = self.frame;
	
	if (fabs(frame.origin.y - originY) < Epsilon) {
		return;
	}
	else {
		frame.origin.y = originY;
		self.frame = frame;
	}
}

- (void)setWidth:(CGFloat)width
{
	CGRect frame = self.frame;
	
	if (fabs(frame.size.width - width) < Epsilon) {
		return;
	}
	else {
		frame.size.width = width;
		self.frame = frame;
	}
}

- (void)setHeight:(CGFloat)height
{
	CGRect frame = self.frame;
	
	if (fabs(frame.size.height - height) < Epsilon) {
		return;
	}
	else {
		frame.size.height = height;
		self.frame = frame;
	}
}

- (void)addScale:(CGFloat)scale
{
	[self addScale:scale forInteger:YES];
}

- (void)addScale:(CGFloat)scale forInteger:(BOOL)forInteger
{
	if (scale <= 0.0)
	{
		return;
	}
	
	CGRect frame = CGRectZero;
 
	if (YES == forInteger)
	{
		frame = CGRectMake(ceil([self originX] * scale),
						   ceil([self originY] * scale),
						   ceil([self width] * scale),
						   ceil([self height] * scale));
	}
	else
	{
		frame = CGRectMake([self originX] * scale,
						   [self originY] * scale,
						   [self width] * scale,
						   [self height] * scale);
	}
	
	self.frame = frame;
}

- (void)addScaleToSubviews:(CGFloat)scale
{
	[self addScaleToSubviews:scale forInteger:YES];
}

- (void)addScaleToSubviews:(CGFloat)scale forInteger:(BOOL)forInteger
{
	for (UIView *subView in self.subviews)
	{
		[subView addScale:scale forInteger:forInteger];
	}
}

- (void)addFontSizeScale:(CGFloat)scale
{
	if (scale <= 0.0)
	{
		return;
	}
	
	if ([self isKindOfClass:[UILabel class]])
	{
		UILabel *selfLabel = (UILabel *)self;
		CGFloat newSize = floor(selfLabel.font.pointSize * scale);
		selfLabel.font = [selfLabel.font fontWithSize:newSize];
	}
}

- (void)addFontSizeScaleToSubviews:(CGFloat)scale
{
	for (UIView *subView in self.subviews)
	{
		[subView addFontSizeScale:scale];
	}
}

@end
